package com.iknight.theory.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iknight.theory.api.TPCurrentService;
import com.iknight.theory.mapper.TPCurrentMapper;
import com.iknight.theory.model.TPCurrentModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TPCurrentServiceImpl implements TPCurrentService<TPCurrentModel, String> {
    @Autowired
    private TPCurrentMapper<TPCurrentModel, String> tpCurrentMapper;

    @Override
    public void save(TPCurrentModel record) {
        if (null != this.tpCurrentMapper.selectOne(record.getObj_id())) {
            this.tpCurrentMapper.update(record);
        } else {
            this.tpCurrentMapper.insert(record);
        }
    }

    @Override
    public void delete(String pkey) {
        this.tpCurrentMapper.delete(pkey);
    }

    @Override
    public TPCurrentModel selectOne(String pkey) {
        return this.tpCurrentMapper.selectOne(pkey);
    }

    @Override
    public PageInfo<TPCurrentModel> selectPage(Map<String, Object> params, int pageStart, int pageSize) {
        PageHelper.startPage(pageStart, pageSize, true);
        List<TPCurrentModel> recordLs = this.tpCurrentMapper.selectByCond(params);
        PageInfo<TPCurrentModel> result = new PageInfo<>(recordLs);
        return result;
    }

    @Override
    public Object getTranCurrentCurse(String tran_id) {
        Map params = new HashMap();
        params.put("tran_id", tran_id);
        List<TPCurrentModel> currentModels = tpCurrentMapper.selectByCond(params);
        List<Map> result = new ArrayList<>();
        for (TPCurrentModel model : currentModels) {
            Map map = new HashMap();
            map.put("phase", model.getPhase_type());
            List<Map> values = new ArrayList<>();
            Map values00 = new HashMap();
            values00.put("time_set", "00:00");
            values00.put("value", model.getValue_0());
            values.add(values00);

            Map values01 = new HashMap();
            values01.put("time_set", "01:00");
            values01.put("value", model.getValue_1());
            values.add(values01);

            Map values02 = new HashMap();
            values02.put("time_set", "02:00");
            values02.put("value", model.getValue_2());
            values.add(values02);

            Map values03 = new HashMap();
            values03.put("time_set", "03:00");
            values03.put("value", model.getValue_3());
            values.add(values03);

            Map values04 = new HashMap();
            values04.put("time_set", "04:00");
            values04.put("value", model.getValue_4());
            values.add(values04);

            Map values05 = new HashMap();
            values05.put("time_set", "05:00");
            values05.put("value", model.getValue_5());
            values.add(values05);

            Map values06 = new HashMap();
            values06.put("time_set", "06:00");
            values06.put("value", model.getValue_6());
            values.add(values06);

            Map values07 = new HashMap();
            values07.put("time_set", "07:00");
            values07.put("value", model.getValue_7());
            values.add(values07);

            Map values08 = new HashMap();
            values08.put("time_set", "08:00");
            values08.put("value", model.getValue_8());
            values.add(values08);

            Map values09 = new HashMap();
            values09.put("time_set", "09:00");
            values09.put("value", model.getValue_9());
            values.add(values09);

            Map values10 = new HashMap();
            values10.put("time_set", "10:00");
            values10.put("value", model.getValue_10());
            values.add(values10);

            Map values11 = new HashMap();
            values11.put("time_set", "11:00");
            values11.put("value", model.getValue_11());
            values.add(values11);

            Map values12 = new HashMap();
            values12.put("time_set", "12:00");
            values12.put("value", model.getValue_12());
            values.add(values12);

            Map values13 = new HashMap();
            values13.put("time_set", "13:00");
            values13.put("value", model.getValue_13());
            values.add(values13);

            Map values14 = new HashMap();
            values14.put("time_set", "14:00");
            values14.put("value", model.getValue_14());
            values.add(values14);

            Map values15 = new HashMap();
            values15.put("time_set", "15:00");
            values15.put("value", model.getValue_15());
            values.add(values15);

            Map values16 = new HashMap();
            values16.put("time_set", "16:00");
            values16.put("value", model.getValue_16());
            values.add(values16);

            Map values17 = new HashMap();
            values17.put("time_set", "17:00");
            values17.put("value", model.getValue_17());
            values.add(values17);

            Map values18 = new HashMap();
            values18.put("time_set", "18:00");
            values18.put("value", model.getValue_18());
            values.add(values18);

            Map values19 = new HashMap();
            values19.put("time_set", "19:00");
            values19.put("value", model.getValue_19());
            values.add(values19);

            Map values20 = new HashMap();
            values20.put("time_set", "20:00");
            values20.put("value", model.getValue_20());
            values.add(values20);

            Map values21 = new HashMap();
            values21.put("time_set", "21:00");
            values21.put("value", model.getValue_21());
            values.add(values21);

            Map values22 = new HashMap();
            values22.put("time_set", "22:00");
            values22.put("value", model.getValue_22());
            values.add(values22);

            Map values23 = new HashMap();
            values23.put("time_set", "23:00");
            values23.put("value", model.getValue_23());
            values.add(values23);
            map.put("values",values);

            result.add(map);
        }
        return result;
    }
}
